package com.springsummer.information.green.webmvc;


public class InternalResourceViewResolver implements ViewResolver {

    public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=ISO-8859-1";


    private Class<?> viewClass = null;

    private String viewClassName = "";

    private String prefix = "";

    private String suffix = ".html";

    private String contentType = DEFAULT_CONTENT_TYPE;

    public InternalResourceViewResolver() {
        if (getViewClass() == null) {
            setViewClass(JstlViewImpl.class);
        }
    }


    public void setViewClass(Class<?> viewClass) {
        this.viewClass = viewClass;
    }

    protected Class<?> getViewClass() {
        return this.viewClass;
    }

    public void setPrefix(String prefix) {
        this.prefix = (prefix != null ? prefix : "");
    }

    protected String getPrefix() {
        return this.prefix;
    }

    public void setSuffix(String suffix) {
        this.suffix = (suffix != null ? suffix : "");
    }

    protected String getSuffix() {
        return this.suffix;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    protected String getContentType() {
        return this.contentType;
    }

    @Override
    public View resolveViewName(String viewName) throws Exception {
        return createView(viewName);
    }

    protected View createView(String viewName) throws Exception {
        Class<?> viewClass = getViewClass();
        View view = (View) viewClass.getDeclaredConstructor().newInstance();

        String contentType = getContentType();
        view.setContentType(contentType);
        view.setPath(getPrefix() + viewName + getSuffix());
        return view;
    }

}



